import {Service} from "adajs";
import {format} from './../../../lib/helper';

class NewsService extends Service {
    defaultData() {
        return {
            isLoad: false,
            newsInfo: {
                author: "",
                columnId: "",
                content: "",
                file: [],
                id: "",
                imgs: "",
                pubulishTime: "",
                siteId: "",
                source: "",
                title: "",
                viceTitle: ""
            },
            id: "",
            error: false,
            msg: ""
        };
    }

    onupdate(current, id) {
        if (id) {
            return this.context.request.post("/api/cms/cmsArticle/get", {id}).then(({data, code, msg}) => {
                current.id = id;
                current.isLoad = true;
                if (code === '1') {
                    current.newsInfo = data;
                    current.newsInfo.time = format(new Date(data.publishTime.replace(/\-/g,"/")), 'yyyy-MM-dd');
                } else {
                    current.error = true;
                    current.msg = msg;
                }
            });
        }
    }
}

export default NewsService;